const { setCookie, getCookie } = require('../src/lib/cookie-store');
const validateInput = require('../src/lib/inputValidator');
const { launch, get_user_details } = require('../src/lib/puppeteer-client');
const { addUser } = require('../src/lib/users-store');

function validate(input) {
    // 添加你的验证逻辑
    return input.trim() !== '';
}
function validateNumber(input) {
    // 添加你的验证逻辑
    return !isNaN(input);
}
const extractUrls = (text) => {
    // 正则表达式用于匹配URL
    const urlRegex = /https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi;
    // 使用match方法提取URL
    const urls = text.match(urlRegex);
    return urls || [];  // 如果没有匹配项，返回空数组
};

(async () => {
    let storeCookie = await getCookie('douyin');
    // console.log(storeCookie);
    if (storeCookie) {
    } else {
        const inputCookie = await validateInput(validate, '请输入cookie：');
        await setCookie('douyin', inputCookie);
        storeCookie = inputCookie;
    }
    // const command = await validateInput(validateNumber, '请输入下一步指令序号：\n1. 添加监控\n2. 删除监控\n3. 启动监控\n4. 退出\n');

    let userStop = false;
    while (!userStop) {
        let inputUrl = await validateInput(validate, '请输入用户主页链接或输入q退出：');
        if (inputUrl === 'q') {
            userStop = true;
            process.exit();
        }
        let urls = extractUrls(inputUrl);
        userDetail = await get_user_details(urls[0], storeCookie, '.douyin.com')
        addUser(userDetail);
        console.log(userDetail.nickname + '录入成功');
    }

})();

// launch(['https://www.douyin.com/user/MS4wLjABAAAAPdhCLJFuFtmLF2aNh528avLfDkDX293B5oLOMR0EfzjVbM5OdNypfBROSn6eQSYw'], storeCookie, '.douyin.com', {
//     aweme_list: function (data) {
//         console.log(data);
//     }
// });
